package cn.edu.gdut.csq.controller;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.LinkedHashMap;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import cn.edu.gdut.csq.entity.Job;
import cn.edu.gdut.csq.entity.PageSet;
import cn.edu.gdut.csq.service.JobService;
import cn.edu.gdut.csq.util.ExcelUtil;
import cn.edu.gdut.csq.util.JsonUtils;

@Controller
@RequestMapping("job")
public class JobController {

	@Autowired
	private JobService jobService;

	@RequestMapping("/list/{pageNow}")
	public String getJobsList(@PathVariable Integer pageNow, Integer pageSize,
			HttpServletRequest req, HttpServletResponse res) {

		System.out.println("pageNow " + pageNow);

		if (pageNow == null) {
			pageNow = 1;
		}
		if (pageSize == null) {
			pageSize = 20;
		}
		PageSet<Job> pagejobs = jobService.getJobs(pageNow, pageSize);
		// System.out.println(jobs);
		req.getSession().setAttribute("pagejobs", pagejobs);
		return "job/joblist";
	}

	@RequestMapping("/{pageNow}")
	@ResponseBody
	public String getJobs(Integer pageNow, Integer pageSize,
			HttpServletRequest req, HttpServletResponse res) {

		System.out.println("pageNow " + pageNow);

		if (pageNow == null) {
			pageNow = 1;
		}
		if (pageSize == null) {
			pageSize = 20;
		}
		PageSet<Job> pagejobs = jobService.getJobs(pageNow, pageSize);

		return JsonUtils.objectToJson(pagejobs);
	}

	@RequestMapping("/type/today/{pageNow}")
	public String getJobsByTime(@PathVariable Integer pageNow,
			Integer pageSize, HttpServletRequest req, HttpServletResponse res) {

		System.out.println("pageNow " + pageNow);

		if (pageNow == null) {
			pageNow = 1;
		}
		if (pageSize == null) {
			pageSize = 20;
		}
		String time = new SimpleDateFormat("yyyy-MM-dd").format(new Date(System
				.currentTimeMillis()));
		System.out.println("job time : " + time);

		PageSet<Job> pagejobs = jobService.getJobs(pageNow, pageSize,
				time);
		// System.out.println(jobs);
		req.getSession().setAttribute("pagejobs", pagejobs);
		return "job/todayjoblist";
	}

	@RequestMapping("/today/excel")
	public void getJobExcel(HttpServletRequest req, HttpServletResponse res) {
		
		
		System.out.println("starting export ...");

		
		String time = new SimpleDateFormat("yyyy-MM-dd").format(new Date(System
				.currentTimeMillis()));
		System.out.println("job time : " + time);
		List<Job> list = jobService.exportJobsbyTime(time);

		LinkedHashMap<String, String> fieldMap = new LinkedHashMap<String, String>();
		
		fieldMap.put("jobName", "职位名称");
		fieldMap.put("companyName", "公司名称");
		fieldMap.put("jobDetailAddress", "公司名称");
		fieldMap.put("publishTime", "发布时间");
		fieldMap.put("queryType", "职位来源");
		fieldMap.put("jobUrl", "职位网址");

		
		String sheetName = new SimpleDateFormat("yyyy-MM-dd").format(new Date(
				System.currentTimeMillis()));
		
		int sheetSize = 65535;
		try {
			ExcelUtil.listToExcel(list, fieldMap, sheetName, sheetSize, res);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

		System.out.println("end ..");
	}

}
